<?php

namespace app\api\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Config;
// use think\facade\Session;
use app\common\model\SystemAdmin;
use app\common\model\User;
use app\common\model\Store;
use app\common\model\ProductType;
use think\facade\Request;
use app\common\model\ProductCollectible;
use app\common\model\ProductBargain;
use app\common\model\ProductBargainUser;
use app\common\model\Product;
use app\common\model\ProductBanner;
use think\facade\Db;

class ApiUserIndex extends ApiBase
{
    public function initialize()
    {
        parent::initialize();
        $this->user = new User;
        $this->Store = new Store;
        $this->ProductType = new ProductType;
        $this->ProductCollectible = new ProductCollectible;
        $this->ProductBargain = new ProductBargain;
        $this->ProductBargainUser = new ProductBargainUser;
        $this->Product = new Product;
        $this->ProductBanner = new ProductBanner;
    }

    // 首页信息
    public function index()
    {
        
        // 存储空间对应的域名
        $domain = config('sysconfig.qiniu_domain');
        $data = request()->post();

        if(empty($data['store_id'])){
            return json(['status' => 0, 'msg' =>"请上传店铺ID"]);
        }

        $list = $this->Store->getOne(['id' =>$data['store_id']]);

        $listProduct = $this->ProductType->getAll(['store_id' =>$data['store_id']],"id,name");

        $data['store_log'] = $list['logo'] = $domain."/".$list['logo'];
        $data['product_type_list'] = $listProduct;
        $data['store_name'] = $list['name'];
        $data['store_introductory'] = $list['introductory'];
        $data['store_address'] = $list['address'];
        $data['store_tel'] = $list['tel'];
        $data['store_phone'] = $list['phone'];

        return json(['status'=> 1,'data'=>$data]);

    }

    //店铺详情与联系地址
    public function getStore()
    {
         // 存储空间对应的域名
         $domain = config('sysconfig.qiniu_domain');

         $data = request()->post();

        if(empty($data['store_id'])){
            return json(['status' => 0, 'msg' =>"请上传店铺ID"]);
        }

        $store_id = $data['store_id'];
 
         $list = $this->Store->getOne(['id' =>$store_id]);
 
 
         if(!empty($list['content'])){
             $list['content'] = str_replace("api_project",$domain."/api_project",$list['content']);
         }
 
        //  if(!empty($list['logo'])){
        //      $list['logo'] = $domain."/".$list['logo'];
        //  }
 
 
        //  if(!empty($list['weichat'])){
        //      $list['weichat'] = $domain."/".$list['weichat'];
        //  }
 
         $list['content'] = str_replace("api_project",$domain."/api_project",$list['content']);
         
         return json(['status'=> 1,'data'=>$list]);
    }


    // 个人信息
    public function getUser()
    {
        $list =  $this->user->getOne(['id' => $this->uid], 'nickname,headimg,number,sex,phone,store_id,identity,name');

        return json(['status'=> 1,'data'=>$list]);
    }


    // 用户基本信息修改
    public function setUserContent()
    {
        
        $post = request()->post();

        $save = [];
        if(!empty($post['name'])){
            $save['name'] = $post['name'];
        }

        if(!empty($post['sex'])){
            $save['sex'] = $post['sex'];
        }

        if(!empty($post['phone'])){
            $save['phone'] = $post['phone'];
        }

        if(empty($save)){
            return json(['status' => 0, 'msg' => '请上次要修改的信息']);
        }

        $this->user->where(['id' => $this->uid])->update($save);
        
        return json(['status' => 1, 'msg' => '修改成功']);

    }

    // 获取收藏列表
    public function collectibleList()
    {

        // 存储空间对应的域名
        $domain = config('sysconfig.qiniu_domain');

        $data = request()->post();

        if(empty($data['store_id'])){
            return json(['status' => 0, 'msg' =>"请上传店铺ID"]);
        }


        if(empty($data['limit'])){
            $data['limit'] = 10;
        }

        if(empty($data['page'])){
            $data['page'] = 1;
        }

        $list = $this->ProductCollectible->getPagelist(['user_id'=>$this->uid,"deleted_at"=>null,"store_id"=>$data['store_id']],"","");

        foreach($list as $key=>$val){
            $getProduct = $this->Product->getOne(['id' =>$val['product_id'],'deleted_at'=>null]);
            if(!empty($getProduct)){
                $list[$key]['product_name'] = $getProduct['name'];
                $list[$key]['product_price'] = $getProduct['price'];
                $list[$key]['sum_number'] = $getProduct['sum_number'];
                $list[$key]['logo'] = $getProduct['logo'];
                $list[$key]['type_name'] = $this->ProductType->where(['id'=>$getProduct['type_id']])->value("name");
            }

        }

        $count = $this->ProductCollectible->countRows(['user_id'=>$this->uid,"deleted_at"=>null]);

        $pages = ceil($count/$data['limit']);

        return json(['status'=> 1,'data'=>$list,'pages'=>$pages,"count"=>$count]);
    }


    // 砍一刀列表
    public function bargainList()
    {
         // 存储空间对应的域名
         $domain = config('sysconfig.qiniu_domain');

         $data = request()->post();

         if(empty($data['store_id'])){
             return json(['status' => 0, 'msg' =>"请上传店铺ID"]);
         }
 
 
         if(empty($data['limit'])){
             $data['limit'] = 10;
         }
 
         if(empty($data['page'])){
             $data['page'] = 1;
         }
 
         $list = $this->ProductBargain->getPagelist(['user_id'=>$this->uid,"deleted_at"=>null,"store_id"=>$data['store_id']],"","");
 
        //  foreach($list as $key=>$val){
        //     $list[$key]['logo'] = $domain."/".$val['logo'];
        //  }
 
         $count = $this->ProductBargain->countRows(['user_id'=>$this->uid,"deleted_at"=>null]);
 
         $pages = ceil($count/$data['limit']);
 
         return json(['status'=> 1,'data'=>$list,'pages'=>$pages,"count"=>$count]);
    }

    // 砍一刀详情
    public function bargainGet()
    {
         // 存储空间对应的域名
         $domain = config('sysconfig.qiniu_domain');

         $data = request()->post();

         if(empty($data['store_id'])){
             return json(['status' => 0, 'msg' =>"请上传店铺ID"]);
         }

        if(empty($data['bargain_id'])){
            return json(['status' => 0, 'msg' =>"请上传砍一刀id"]);
        }
 
        $list = $this->ProductBargain->getOne(['id' =>$data['bargain_id'],'deleted_at'=>null]);

        if(empty($list)){
            return json(['status' => 0, 'msg' =>"砍单不存在"]);
        }

         
        // $list['logo'] = $domain."/".$list['logo'];
 
 
         $list['content'] = str_replace("api_project",$domain."/api_project",$list['content']);
 
        
 
         return json(['status'=> 1,'data'=>$list]);
    }


    // 砍一刀邀约
    public function addBargain()
    {
        // 存储空间对应的域名
        $domain = config('sysconfig.qiniu_domain');

        $data = request()->post();

        if(empty($data['store_id'])){
            return json(['status' => 0, 'msg' =>"请上传店铺ID"]);
        }

        if(empty($data['bargain_id'])){
            return json(['status' => 0, 'msg' =>"请上传砍一刀id"]);
        }

        $list = $this->ProductBargain->getOne(['id' =>$data['bargain_id'],'deleted_at'=>null]);

        if(empty($list)){
            return json(['status' => 0, 'msg' =>"砍单不存在"]);
        }

        if($this->uid == $list['user_id']){
            return json(['status' => 0, 'msg' =>"不能给自己发布的订单砍单"]);
        }

        // 砍单人数
        $bargain_people_num = $list['bargain_people_num'] +1;

        // 砍单金额 
        $bargain_price =  bcdiv($list['bargain_price'], $list['bargain_people'],2);

        // 判断此账号是否已经砍单过

        $findUser = $this->ProductBargainUser->getOne(['product_borgain_id' =>$data['bargain_id'],'deleted_at'=>null,"user_id"=>$this->uid]);

        if(!empty($findUser)){
            return json(['status' => 0, 'msg' =>"您已经砍过单了"]);
        }


        if($bargain_people_num  > $list['bargain_people']){
            return json(['status' => 0, 'msg' =>"砍单人数已经满了"]);
        }

        // 开启事务
        Db::startTrans();
        try {

            $userList = $this->user->getOne(['id' => $this->uid], 'nickname,headimg,number,sex,phone,store_id,identity,name');
        
            $add['user_id'] = $this->uid;
            $add['nickname'] =  $userList['nickname'];
            $add['headimg'] =  $userList['headimg'];
            $add['product_borgain_id'] = $data['bargain_id'];
            $add['price'] = $bargain_price;
            $add['created_at'] = date("Y-m-d H:i:s");
            $add['update_at'] = date("Y-m-d H:i:s");
    
            $res = $this->product_bargain_user->create($sadd);
    
            $save['bargain_people_num'] = $bargain_people_num;
    
            if($bargain_people_num == $list['bargain_people']){
                $save['bargain_price2'] = $list['bargain_price'];
            }else{
                $save['bargain_price2'] = $bargain_price * $bargain_people_num;
            }
    
            $resUpdate = $this->ProductBargain->where(['id'=>$data['bargain_id']])->update($save);
            // 提交事务
            Db::commit();
            return json(['status' => 1, 'msg' => '砍单成功']);
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['status' => 0, 'msg' =>$e->getMessage()]);
        }


    }
    
}
